package system

import (
	"MoSkeleton/framework"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/services"
	s1 "MoSkeleton/web/pogos/request/system"
	"MoSkeleton/web/v1/http/base"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
)

type SysNoticeCtrl struct {
	base.BaseCtrl
}

func (s *SysNoticeCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleSysNoticeMsg)
	return msg
}

// GetNoticePage 公告列表
// @Tags 公告管理
// @Summary 公告列表
// @Security ApiKeyAuth
// @Param data query system.SysNoticePageReq true "分页获取公告列表"
// @Success 200 {object} response.Response{data=response.PageResult} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": "服务器错误"}"
// @Router /system/notice/list [get]
func (n *SysNoticeCtrl) GetNoticePage(c *gin.Context) {
	pageReq := &s1.SysNoticePageReq{}
	if err := c.ShouldBindQuery(pageReq); err != nil {
		framework.MoLogger.Error("获取公告列表失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	pageReq.ShouldBindParams(c)
	pageResult, err := services.ServiceGroupInst.SystemGroupInst.SysNoticeServiceInst.GetNoticePage(pageReq)
	if err != nil {
		framework.MoLogger.Error("获取公告列表失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_NoticeGetListFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
}

// GetNoticeById 公告详情
// @Tags 公告管理
// @Summary 公告详情
// @Security ApiKeyAuth
// @Param data query system.SysNoticePageReq true "获取公告详情"
// @Success 200 {object} response.Response{data=system.SysNoticeItemResp} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": "服务器错误"}"
// @Router /system/notice/{id} [get]
func (n *SysNoticeCtrl) GetNoticeById(c *gin.Context) {
	idStr := c.Param("id")
	if idStr == "" {
		framework.MoLogger.Error("获取公告详情失败")
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	noticeDetailItemResp, err := services.ServiceGroupInst.SystemGroupInst.SysNoticeServiceInst.GetNoticeById(idStr)
	if err != nil {
		framework.MoLogger.Error("获取公告详情失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommSelectFailCode, consts.TKey_NoticeGetFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, noticeDetailItemResp)
}

// AddNotice 新增公告
// @Tags 公告管理
// @Summary 新增公告
// @Security ApiKeyAuth
// @Param data body system.SysNoticeAddReq true "新增公告"
// @Success 200 {object} response.Response{data=system.SysNoticeItemResp} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": "服务器错误"}"
// @Router /system/notice [post]
func (n *SysNoticeCtrl) AddNotice(c *gin.Context) {
	noticeAddReq := &s1.SysNoticeAddReq{}
	if err := c.ShouldBindJSON(noticeAddReq); err != nil {
		framework.MoLogger.Error("新增公告失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	noticeAddReq.CreatedBy = utils.GetUsernameFromGin(c)
	noticeAddReq.UpdatedBy = noticeAddReq.CreatedBy
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysNoticeServiceInst.AddNotice(noticeAddReq); err != nil {
		framework.MoLogger.Error("新增公告失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_NoticeAddFailMsg)
		return
	} else {
		if !ok {
			framework.MoLogger.Error("新增公告失败")
			response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.TKey_NoticeAddFailMsg)
			return
		}
		response.SuccessAsI18n(c, consts.TKey_CommCreatSuccessMsg, nil)
	}
}

// UpdateNotice 修改公告
// @Tags 公告管理
// @Summary 修改公告
// @Security ApiKeyAuth
// @Param data body system.SysNoticeUpdateReq true "修改公告"
// @Success 200 {object} response.Response{data=system.SysNoticeItemResp} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": "服务器错误"}"
// @Router /system/notice [put]
func (n *SysNoticeCtrl) UpdateNotice(c *gin.Context) {
	noticeUpdateReq := &s1.SysNoticeUpdateReq{}
	if err := c.ShouldBindJSON(noticeUpdateReq); err != nil {
		framework.MoLogger.Error("修改公告失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	noticeUpdateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysNoticeServiceInst.UpdateNotice(noticeUpdateReq); err != nil {
		framework.MoLogger.Error("修改公告失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_NoticeUpdateFailMsg)
		return
	} else if !ok {
		framework.MoLogger.Error("修改公告失败")
		response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_NoticeUpdateFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
}

// DeleteNotices 删除公告
// @Tags 公告管理
// @Summary 删除公告
// @Security ApiKeyAuth
// @Param id path string true "id, 删除公告"
// @Success 200 {object} response.Response{data=system.SysNoticeItemResp} "{"code": 200, "data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"code": 500, "msg": "服务器错误"}"
// @Router /system/notice/{id} [delete]
func (n *SysNoticeCtrl) DeleteNotices(c *gin.Context) {
	idStr := c.Param("id")
	if len(idStr) == 0 {
		framework.MoLogger.Error("删除公告失败")
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	idStrs := strings.Split(idStr, ",")
	if ok, err := services.ServiceGroupInst.SystemGroupInst.SysNoticeServiceInst.DeleteNotices(&idStrs); err != nil {
		framework.MoLogger.Error("删除公告失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_NoticeDeleteFailMsg)
		return
	} else if !ok {
		framework.MoLogger.Error("删除公告失败")
		response.FailMsgAsI18n(c, consts.CommDeleteFailCode, consts.TKey_NoticeDeleteFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommDeleteSuccessMsg, nil)
}
